package com.handinglian.system.service.impl;

import com.handinglian.common.utils.FastJsonUtil;
import com.handinglian.system.dto.MenuDto;
import com.handinglian.system.dto.PermissionDto;
import com.handinglian.system.entity.Menu;
import com.handinglian.system.mapper.MenuMapper;
import com.handinglian.system.service.MenuService;
import com.handinglian.system.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("menuService")
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private PermissionService permissionService;

    @Override
    public List<MenuDto> inquireMenuPermissionList() {
        List<PermissionDto> permissionDtos = permissionService.inquirePermissionList();

        //按菜单级别查询出相关菜单信息
        List<MenuDto> menuOneLevelDto = inquireMenuByLevel(1, permissionDtos);
        List<MenuDto> menuTwoLeveleDto = inquireMenuByLevel(2, permissionDtos);
        List<MenuDto> menuThreeLevelDto = inquireMenuByLevel(3, permissionDtos);
        List<MenuDto> menuFourLevelDto = inquireMenuByLevel(4, permissionDtos);
        List<MenuDto> menuFiveLevelDto = inquireMenuByLevel(5, permissionDtos);

        //添加二级菜单
        for (MenuDto menuDto : menuOneLevelDto){
            for (MenuDto menuSubDto : menuTwoLeveleDto){
                if (menuDto.getMenuId().equals(menuSubDto.getParentId())){
                    menuDto.addMenu(menuSubDto);
                }
            }
        }

        //添加三级菜单
        for (MenuDto menuDto : menuTwoLeveleDto){
            for (MenuDto menuSubDto : menuThreeLevelDto){
                if (menuDto.getMenuId().equals(menuSubDto.getParentId())){
                    menuDto.addMenu(menuSubDto);
                }
            }
        }

        //添加四级菜单
        for (MenuDto menuDto : menuThreeLevelDto){
            for (MenuDto menuSubDto : menuFourLevelDto){
                if (menuDto.getMenuId().equals(menuSubDto.getParentId())){
                    menuDto.addMenu(menuSubDto);
                }
            }
        }

        //添加五级菜单
        for (MenuDto menuDto : menuFourLevelDto){
            for (MenuDto menuSubDto : menuFiveLevelDto){
                if (menuDto.getMenuId().equals(menuSubDto.getParentId())){
                    menuDto.addMenu(menuSubDto);
                }
            }
        }

        return menuOneLevelDto;
    }


    @Override
    public List<MenuDto> inquireMenuByLevel(Integer level, List<PermissionDto> permissionDtos) {
        List<Menu> menuList = menuMapper.findByLevel(level);
        List<MenuDto> menuDtos = FastJsonUtil.ListToList(menuList, MenuDto.class);

        menuDtos.forEach(menuDto -> {
            permissionDtos.forEach(permissionDto -> {
                if (permissionDto.getMenuId().equals(menuDto.getMenuId())){
                    menuDto.getPermissionDtos().add(permissionDto);
                }
            });
        });
        return menuDtos;
    }
}
